package com.yvon.maple.service.msg.core.websocket.processor;

import cn.hutool.json.JSONUtil;
import com.yvon.maple.service.msg.core.websocket.message.SubscribeMessage;
import com.yvon.maple.service.msg.core.websocket.util.SessionUtil;
import com.yvon.maple.service.msg.core.websocket.message.ResponseMessage;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;

/**
 * @author : Yvon
 * @since : 2021-12-03
 */
public class SubscribeProcessor extends Processor<SubscribeMessage> {

    @Override
    public void exec(Channel channel, String msg) {
        SubscribeMessage message = parse(msg);
        String type = message.getType();
        String subscriber = message.getSubscriber();
        SessionUtil.subscribe(type, channel);
        ResponseMessage  responseMessage = new ResponseMessage();
        responseMessage.setContent("[" + subscriber + "]:[" + type + "]订阅成功");
        channel.writeAndFlush(new TextWebSocketFrame(JSONUtil.toJsonStr(responseMessage)));
    }
}
